package com.edu.exam.enums.operations;

import com.edu.exam.enums.core.BaseEnum;

/**
 * 批量操作状态枚举
 *
 * 管理批量操作的执行状态流程，包括待执行、执行中、已完成、执行失败、已取消、已暂停、执行超时等状态
 *
 * @author System
 * @version 1.0.0
 */
public enum BatchStatus implements BaseEnum {

    /**
     * 待执行
     */
    PENDING("PENDING", "待执行"),

    /**
     * 执行中
     */
    RUNNING("RUNNING", "执行中"),

    /**
     * 已完成
     */
    COMPLETED("COMPLETED", "已完成"),

    /**
     * 执行失败
     */
    FAILED("FAILED", "执行失败"),

    /**
     * 已取消
     */
    CANCELLED("CANCELLED", "已取消"),

    /**
     * 已暂停
     */
    PAUSED("PAUSED", "已暂停"),

    /**
     * 执行超时
     */
    TIMEOUT("TIMEOUT", "执行超时");

    private final String code;
    private final String description;

    BatchStatus(String code, String description) {
        this.code = code;
        this.description = description;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getDescription() {
        return description;
    }

    /**
     * 从代码创建枚举实例
     *
     * @param code 枚举代码
     * @return 对应的枚举实例
     * @throws IllegalArgumentException 如果代码无效
     */
    public static BatchStatus fromCode(String code) {
        if (code == null || code.trim().isEmpty()) {
            throw new IllegalArgumentException("Batch status code cannot be null or empty");
        }
        for (BatchStatus status : values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        throw new IllegalArgumentException("Unknown batch status code: " + code);
    }

    /**
     * 根据描述查找枚举值
     *
     * @param description 显示描述
     * @return 对应的枚举值
     * @throws IllegalArgumentException 如果描述无效
     */
    public static BatchStatus fromDescription(String description) {
        if (description == null || description.trim().isEmpty()) {
            throw new IllegalArgumentException("Batch status description cannot be null or empty");
        }
        for (BatchStatus status : values()) {
            if (status.getDescription().equals(description)) {
                return status;
            }
        }
        throw new IllegalArgumentException("Unknown batch status description: " + description);
    }

    @Override
    public String toString() {
        return this.code + "(" + this.description + ")";
    }
}